package com.voxel.sense.infomsg.module.firm.details.general;

import android.graphics.Paint;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.example.com.common.utils.FileUtils;
import com.example.com.common.utils.L;
import com.voxel.sense.infomsg.R;
import com.voxel.sense.infomsg.data.bean.FirmTableInfoBean;
import com.voxel.sense.infomsg.view.holders.ExpandViewHolder;
import com.voxel.sense.infomsg.widget.CommonImagePagerActivity;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import cyy.example.com.facotry.constants.EventBusConstants;
import cyy.example.com.facotry.model.EventEntity;

/**
 * Created by ChenYiYao on 2018/3/1.
 */

public class FirmGeneralExpandAdapter extends BaseMultiItemQuickAdapter<FirmTableInfoBean, ExpandViewHolder<FirmTableInfoBean>> {
    public final List<FirmTableInfoBean> mDataList;

    public FirmGeneralExpandAdapter(List<FirmTableInfoBean> data) {
        super(data);
        mDataList = data;
        addItemType(FirmTableInfoBean.TYPE_HEAD_EXPAND, R.layout.item_recycler_firm_table_head);
        addItemType(FirmTableInfoBean.TYPE_URL, R.layout.item_recycler_firm_table_head);
        addItemType(FirmTableInfoBean.TYPE_HEAD, R.layout.item_recycler_firm_table_head);
        addItemType(FirmTableInfoBean.TYPE_HEAD_SECOND, R.layout.item_recycler_firm_table_head_second);
        addItemType(FirmTableInfoBean.TYPE_NORMAL, R.layout.item_recycler_firm_table_third);
    }


    @Override
    protected ExpandViewHolder onCreateDefViewHolder(ViewGroup parent, int viewType) {
        ExpandViewHolder holder;
        View inflate;
        switch (viewType) {
            case FirmTableInfoBean.TYPE_HEAD_EXPAND:
            case FirmTableInfoBean.TYPE_URL:
            case FirmTableInfoBean.TYPE_HEAD:
                inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recycler_firm_table_head, parent, false);
                holder = new ViewHolder(inflate);
                break;
            case FirmTableInfoBean.TYPE_HEAD_SECOND:
                inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recycler_firm_table_head_second, parent, false);
                holder = new ViewholderHeadSecond(inflate);
                break;
            case FirmTableInfoBean.TYPE_NORMAL:
                inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recycler_firm_table_third, parent, false);
                holder = new ViewholderThird(inflate);
                break;
            default:
                inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recycler_firm_table_head, parent, false);
                holder = new ViewHolder(inflate);
                break;
        }
        return holder;
    }

    @Override
    protected void convert(final ExpandViewHolder<FirmTableInfoBean> helper, final FirmTableInfoBean item) {
        switch (item.getItemType()) {
            case FirmTableInfoBean.TYPE_HEAD_EXPAND:
            case FirmTableInfoBean.TYPE_URL:
            case FirmTableInfoBean.TYPE_HEAD:
                helper.onBind(item);
                if (item.getItemType() == FirmTableInfoBean.TYPE_HEAD_EXPAND) {
                    //设置TYPE_HEAD_EXPAND类型的item点击展开和收缩事件
                    helper.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            L.d("item点击事件:" + item.getSubItems().size());
                            int adapterPosition = helper.getAdapterPosition();
                            if (item.isExpanded()) {
                                collapse(adapterPosition, false);
                                helper.changeExpandIcon(R.mipmap.ic_drop_selected_icon);
                            } else {
                                expand(adapterPosition, false);
                                helper.changeExpandIcon(R.mipmap.ic_drop_unselected_icon);
                            }

                        }
                    });
                }

                break;
            case FirmTableInfoBean.TYPE_HEAD_SECOND:
                helper.onBind(item);
                break;
            case FirmTableInfoBean.TYPE_NORMAL:
                helper.onBind(item);
                break;
        }
    }

    /**
     * 替换为一个新的集合，其中包括了清空
     *
     * @param dataList 一个新的集合
     */
    public void replace(Collection<FirmTableInfoBean> dataList) {
        mDataList.clear();
        if (dataList == null || dataList.size() == 0)
            return;
        mDataList.addAll(dataList);
        notifyDataSetChanged();
    }


    private static class ViewHolder extends ExpandViewHolder<FirmTableInfoBean> {
        TextView tv_desc;
        TextView tv_value;
        View root;

        private ViewHolder(View itemView) {
            super(itemView);
            tv_desc = itemView.findViewById(R.id.tv_desc);
            tv_value = itemView.findViewById(R.id.tv_value);
            root = itemView;
        }


        public void onBind(final FirmTableInfoBean bean) {
            tv_desc.setText(bean.getDescribe());
            tv_value.setText(bean.getValue());
            if (FirmTableInfoBean.TYPE_HEAD_EXPAND == bean.getType()) {
//                root.setBackground(Factory.app().getResources().getDrawable(R.color.table_head_bg_grey));
                int resId = (bean.isExpanded()) ? R.mipmap.ic_drop_selected_icon : R.mipmap.ic_drop_unselected_icon;
                changeExpandIcon(resId);
            } else {
//                root.setBackground(Factory.app().getResources().getDrawable(R.color.table_second_bg_grey));
                tv_value.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
            }

            if (FirmTableInfoBean.TYPE_URL == bean.getType()) {
                //由于这里是连接，文本不需要直接显示连接地址
                tv_value.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
                tv_value.getPaint().setAntiAlias(true);//抗锯齿
                if (!TextUtils.isEmpty(bean.getValue())) {
                    tv_value.setText("点击查看");
                }
                //设置查看事件
                LookFile(bean);
            } else {
                tv_value.getPaint().setFlags(0);
            }
        }

        @Override
        public void changeExpandIcon(int ResId) {
            tv_value.setCompoundDrawablesWithIntrinsicBounds(0, 0, ResId, 0);
        }

        private void LookFile(final FirmTableInfoBean bean) {
            L.d("info_picture:" + bean.getValue());
            tv_value.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!TextUtils.isEmpty(bean.getValue())) {
                        String value = bean.getValue();
                        ArrayList<String> list = new ArrayList<>();
                        list.add(bean.getValue());

                        String fileExtension = FileUtils.getFileExtension(value);
                        if ("pdf".equals(fileExtension)) {
                            //pdf文件需要下载，发送通知进行下载
                            EventBus.getDefault().post(new EventEntity(EventBusConstants.DOWN_FIRM_LICENSE, bean.getValue()));
                        } else {
                            //先判断是图片还是psd文件，然后再根据类型进行操作
                            CommonImagePagerActivity.showImages(root.getContext(), list, 0);
                        }
                    }
                }
            });
        }
    }


    static class ViewholderHeadSecond extends ExpandViewHolder<FirmTableInfoBean> {
        TextView tv_desc;

        private ViewholderHeadSecond(View itemView) {
            super(itemView);
            tv_desc = itemView.findViewById(R.id.tv_desc);
        }

        @Override
        public void onBind(FirmTableInfoBean bean) {
            tv_desc.setText(bean.getDescribe());
        }

        @Override
        public void changeExpandIcon(int id) {

        }
    }

    static class ViewholderThird extends ExpandViewHolder<FirmTableInfoBean> {
        TextView tv_desc;
        TextView tv_value;

        private ViewholderThird(View itemView) {
            super(itemView);
            tv_desc = itemView.findViewById(R.id.tv_desc);
            tv_value = itemView.findViewById(R.id.tv_value);
        }

        @Override
        public void onBind(FirmTableInfoBean bean) {
            tv_desc.setText(bean.getDescribe());
            tv_value.setText(bean.getValue());
        }

        @Override
        public void changeExpandIcon(int id) {

        }
    }

}
